<?php
/* CLAUSES SPECIFIC FOR MYSQL */
require_once(dirname(__FILE__)."/clauses/MySQLAlterClause.php");
require_once(dirname(__FILE__)."/clauses/MySQLColumnDefinitionClause.php");
/* CONSTANTS SPECIFIC FOR MYSQL */
require_once(dirname(__FILE__)."/constants/MySQLComparisonOperators.php");
require_once(dirname(__FILE__)."/constants/MySQLLogicalOperators.php");

/**
 * Model for table-specific MySQL queries
 */
class MySQLTableQuery extends AbstractSQLTableQuery {
	/**
	 * Alters current table
	 * 
	 * @param MySQLAlterClause $objSQLAlterClause
	 * @return string
	 */
	public function alter($objSQLAlterClause) {
		return "ALTER TABLE ".$this->getTableName()." ".$objSQLAlterClause->toString();
	}

	/**
	 * Creates current table.
	 * 
	 * Note: since SQLColumnDefinition is abstract type, when $objSQLCreateClause->setColumn(...) is called, argument must be MySQLColumnDefinition instead
	 * 
	 * @param SQLCreateClause $objSQLCreateClause
	 * @param string $strTableType
	 * @return string
	 */
	public function create($objSQLCreateClause, $strNewTableType="") {
		return ""																						."\r\n".
		"CREATE TABLE ".$this->getTableName()															."\r\n".
		"(".$objSQLCreateClause->toString().")" 														."\r\n".
		($strNewTableType?"ENGINE=".$strNewTableType:"");
	}
	
	/**
	 * Shows columns information for current table
	 * 
	 * @return string
	 */
	public function describe() {
		return "DESCRIBE TABLE ".$this->getTableName();
	}
	
	/**
	 * Drops current table.
	 * 
	 * @return string
	 */
	public function drop() {
		return "DROP TABLE ".$this->getTableName();
	}
	
	/**
	 * Renames current table
	 * 
	 * @param string $strNewTableName
	 * @return string
	 */
	public function rename($strNewTableName) {
		return "RENAME TABLE ".$this->getTableName()." TO ".$strNewTableName;
	}
	
	/**
	 * Empties current table.
	 * 
	 * @return string
	 */
	public function truncate() {
		return "TRUNCATE TABLE ".$this->getTableName();
	}
	
	/**
	 * Selects data from current table, as well as others, if joined.
	 * 
	 * @param SQLOptionsClause $objSQLOptionsClause
	 * @param SQLColumnsClause $objSQLColumnsClause
	 * @param SQLJoinsClause $objSQLJoinsClause
	 * @param SQLConditionsClause $objSQLConditionsClause
	 * @param SQLGroupByClause $objSQLGroupByClause
	 * @param SQLHavingClause $objSQLHavingClause
	 * @param SQLOrderByClause $objSQLOrderByClause
	 * @param SQLLimitClause $objSQLLimitClause
	 */
	public function select($objSQLOptionsClause=null, $objSQLColumnsClause=null, $objSQLJoinsClause=null, $objSQLConditionsClause=null, $objSQLGroupByClause=null, $objSQLHavingClause=null, $objSQLOrderByClause=null, $objSQLLimitClause=null) {
		return "".
		"SELECT" 																						."\r\n".
		($objSQLOptionsClause?$objSQLOptionsClause->toString():"") 										."\r\n".
		($objSQLColumnsClause?$objSQLColumnsClause->toString():"*") 									."\r\n".
		"FROM ".$this->getTableName()																	."\r\n".
		($objSQLJoinsClause?$objSQLJoinsClause->toString():"")											."\r\n".		
		"WHERE" 																						."\r\n".	
		"".($objSQLConditionsClause?$objSQLConditionsClause->toString():"1")							."\r\n".
		($objSQLGroupByClause?"GROUP BY ".$objSQLGroupByClause->toString():"") 							."\r\n".
		($objSQLHavingClause?"HAVING ".$objSQLHavingClause->toString():"")								."\r\n".
		"ORDER BY ".($objSQLOrderByClause?$objSQLOrderByClause->toString():"NULL")						."\r\n".
		($objSQLLimitClause?"LIMIT ".$objSQLLimitClause->toString():"");
	}
	
	/**
	 * Inserts values into current table. 
	 * 
	 * Note: use null for $objSQLColumnsClause if you wish columns list be automatically generated from inserts.
	 * 
	 * @param SQLColumnsClause $objSQLColumnsClause
	 * @param SQLInsertsClause $objSQLInsertsClause
	 * @return string
	 */
	public function insert($objSQLColumnsClause, $objSQLInsertsClause) {
		return "".
		"INSERT INTO ".$this->getTableName()																."\r\n".
		"(".($objSQLColumnsClause?$objSQLColumnsClause->toString():$objSQLInsertsClause->getColumns()).")"	."\r\n".
		"VALUES"																							."\r\n".
		$objSQLInsertsClause->getRows($objSQLColumnsClause?$objSQLColumnsClause->toArray():array());
	}
	
	/**
	 * Replaces values into current table.
	 * 
	 * Note: use null for $objSQLColumnsClause if you wish columns list be automatically generated from inserts.
	 * 
	 * @param SQLColumnsClause $objSQLColumnsClause
	 * @param SQLInsertsClause $objSQLInsertsClause
	 * @return string
	 */
	public function replace($objSQLColumnsClause, $objSQLInsertsClause) {
		return "".
		"REPLACE INTO ".$this->getTableName()																."\r\n".
		"(".($objSQLColumnsClause?$objSQLColumnsClause->toString():$objSQLInsertsClause->getColumns()).")"	."\r\n".
		"VALUES"																							."\r\n".
		$objSQLInsertsClause->getRows($objSQLColumnsClause?$objSQLColumnsClause->toArray():array());
	}
	
	/**
	 * Updates values from current table.
	 * 
	 * @param SQLUpdatesClause $objSQLUpdatesClause
	 * @param SQLConditionsClause $objSQLConditionsClause
	 * @return string
	 */
	public function update($objSQLUpdatesClause, $objSQLConditionsClause=null) {
		return "".
		"UPDATE ".$this->getTableName()																	."\r\n".		
		"SET ".$objSQLUpdatesClause->toString()															."\r\n".
		"WHERE ".($objSQLConditionsClause?$objSQLConditionsClause->toString():"1");
	}
	
	/**
	 * Deletes values from current table.
	 * 
	 * (non-PHPdoc)
	 * @see AbstractSQLTableQuery::delete()
	 * @param SQLUpdatesClause $objSQLUpdatesClause
	 * @param SQLConditionsClause $objSQLConditionsClause
	 * @return string
	 */
	public function delete($objSQLConditionsClause) {
		return "".
		"DELETE FROM ".$this->getTableName()															."\r\n".
		"WHERE ".$objSQLConditionsClause->toString();
	}
}